package com.rockville.ufone.rssdata;

import java.io.Serializable;
import java.util.Date;

import com.sun.syndication.feed.rss.Channel;
import com.sun.syndication.feed.rss.Item;

/**
 * @author Ahsan
 * @modified Qaiser Suleman
 */
public class ChannelData implements Serializable {

    private String heading = null;
    private String link = null;
    private Channel channel = null;
    private Date lastUpdatedTime = null;
    private String strFeedDefaultImage=null;

    public ChannelData() {
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public String getStrFeedDefaultImage() {
        return strFeedDefaultImage;
    }
    public void setStrFeedDefaultImage(String strImage) {
        this.strFeedDefaultImage = strImage;
    }
    public String getHeading() {
        return heading;
    }
    public void setHeading(String heading) {
        this.heading = heading;
        //this.heading = com.rockville.util.StringHelper.unescapeHTML(com.rockville.util.StringHelper.removeHtmlTags(heading));
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public Date getLastUpdatedTime() {
        return lastUpdatedTime;
    }

    public void setLastUpdatedTime(Date lastUpdatedTime) {
        this.lastUpdatedTime = lastUpdatedTime;
    }

    @Override
    public String toString() {        
        String strThisObj="heading: "+this.getHeading()+"\nlink:"+this.getLink()+"\nlast update time: "+this.getLastUpdatedTime();
        java.util.List<Item> setItem=this.getChannel().getItems();
        java.util.Iterator it_items=setItem.iterator();
        
        StringBuffer strbuff=new StringBuffer();
        for (; it_items.hasNext();) {
            try {

                Item item = (Item) it_items.next();
                String ititle = "", idescription = "";
                try {
                    ititle = item.getTitle();
                    ititle = toUnicode(ititle);
                } catch (Exception exGetTitle) {
                }
                
                try {
                    idescription = "" + item.getDescription().toString();
                } catch (Exception exGetIlink) {
                }

                strbuff.append("\ntitle:" + ititle + "\nDescription: " + idescription);
            } catch (Exception exGetString) {
            }
        }

        return strThisObj+strbuff.toString();
        //return super.toString();
    }
    
    /**
     * <pre>
     * Converts a string to a unicode string.
     * Allows the browser to display other languages aswell.</pre>
     * @param strToConvert
     * @return
     */
    private String toUnicode(String strToConvert) {
        String temp = "";
        char[] charr;        
        charr = strToConvert.toCharArray();        
        for (int j = 0; j < charr.length; j++) {
            int c = (int) charr[j];
            if (c > 250) {
                temp = temp + "&#" + Integer.toString(c) + ";";
            } else {
                temp += charr[j];
            }
        }        
        return temp;
    }
}
